package com.tiantianyuegou.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.activity.BaseActivity;
import com.tiantianyuegou.R;
import com.tiantianyuegou.activity.MyOrderDetailsActivity;
import com.tiantianyuegou.activity.RefundDetailsActivity;
import com.tiantianyuegou.activity.SettingBalanceActivity;
import com.tiantianyuegou.adapter.OrderAdapter;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.enums.PayType;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.model.MyGoodsModel;
import com.tiantianyuegou.model.PayOrderInfo;
import com.tiantianyuegou.model.ShopModel;
import com.tiantianyuegou.pay.AccountPay;
import com.tiantianyuegou.pay.OnMyPayListner;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.SelfUtils;
import com.tiantianyuegou.view.MyDialogTextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huyang on 2016/1/19 0019.
 * 我的订单列表
 */
public class OrderListFragment extends Fragment implements AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener, OnMyPayListner {

    private static final String ARG_POSITION = "position";

    private ImageView img_hint;
    private TextView text_hint;

    private int position;
    private View rootView;
    private PullToRefreshListView refreshListView;
    private BaseActivity activity;
    private List<JSONObject> shopList;
    private OrderAdapter mAdapter;
    public JSONArray shopArray;
    public JSONArray st7;
    private JSONArray aShopList;
    private String TAG = "OrderListFragment";
    private LoadingDialog loadingDialog;
    private int page = 1;
    private boolean isLoadMore = false;
    private ShopModel shopModel;
    private MyGoodsModel myGoodsModel;
    private int id, item;
    private String strName, strNum;
    BaseActivity mBaseActivity;

    public static OrderListFragment newInstance(int position, JSONArray shopList) {
        OrderListFragment f = new OrderListFragment();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        f.setArguments(b);
        return f;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Log.e(TAG, "OrderListFragment     " + "onCreate");
        position = getArguments().getInt(ARG_POSITION);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        activity = (BaseActivity) getActivity();

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_orderlist, container,
                    false);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        loadingDialog = new LoadingDialog(getActivity());
        init();
        initData();
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if ("member".equals(SPHelper.getTYPE())) {
            loadOrder();
        } else if ("seller".equals(SPHelper.getTYPE())) {
            loadSeller();
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ("member".equals(SPHelper.getTYPE())) {
            loadOrder();
        } else if ("seller".equals(SPHelper.getTYPE())) {
            loadSeller();
        }
    }

    public void init() {

        img_hint = (ImageView) rootView.findViewById(R.id.img_hint);
        text_hint = (TextView) rootView.findViewById(R.id.text_hint);

        refreshListView = (PullToRefreshListView) rootView.findViewById(R.id.refreshView);
        refreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        refreshListView.setOnRefreshListener(this);

    }

    public void initData() {
        shopList = new ArrayList<>();
        mAdapter = new OrderAdapter(getActivity());
        refreshListView.setAdapter(mAdapter);
        myInterface();
        refreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                shopList.get(position - 1);
                try {
                    String status = shopList.get(position - 1).getString("toStatus");
                    if ("7".equals(status)) {
                        Intent intent = new Intent(getActivity(), RefundDetailsActivity.class);
                        intent.putExtra("shopList", shopList.get(position - 1).toString());
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), MyOrderDetailsActivity.class);
                        intent.putExtra("shopList", shopList.get(position - 1).toString());
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void load(int position) {
        this.position = position;

    }

    //订单管理
    private void loadOrder() {

        RequestParams params = new RequestParams();

        loadingDialog.setMsg("请稍等");
        loadingDialog.show();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        if (position == 5) {
            params.addBodyParameter("status", "6");
        } else {
            params.addBodyParameter("status", position + "");
        }
        params.addBodyParameter("page", page + "");
        activity.loadData(HttpRequest.HttpMethod.POST, ApiDefine.ORDER_LIST, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                loadingDialog.dismiss();
                refreshListView.onRefreshComplete();
                String response = responseInfo.result;
                Log.e("TAG", "order status :" + position);
                Log.e(TAG, "load all order     " + response);
                try {
                    JSONObject object = new JSONObject(response);
                    if (object.getString("code").equals("100")) {
                        JSONArray aShopList = object.getJSONArray("list");
                        if (aShopList.length() > 0) {
                            if (isLoadMore) {
                                isLoadMore = false;
                            } else {
                                shopList.clear();
                            }
                            shopArray = aShopList;
                            shops(aShopList);
                            for (int shop = 0; shop < aShopList.length(); shop++) {
                                try {
                                    shopList.add(aShopList.getJSONObject(shop));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
//                            refreshListView.setAdapter(mAdapter);
                            mAdapter.setData(shopList, position);
                            mAdapter.notifyDataSetChanged();
                            item = mAdapter.getPosition();
                        } else {
                            if (isLoadMore) {
                                isLoadMore = false;
                                page--;
                            } else {
                                View view = LayoutInflater.from(getActivity()).inflate(R.layout.view_no_data, null);
                                refreshListView.setEmptyView(view);
                            }
                        }
                    } else {
                        if (isLoadMore) {
                            isLoadMore = false;
                            page--;
                        } else {
                            View view = LayoutInflater.from(getActivity()).inflate(R.layout.view_no_data, null);
                            refreshListView.setEmptyView(view);
                            shopList.clear();
                            mAdapter.setData(shopList, position);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                } catch (Exception e) {
                    if (isLoadMore) {
                        isLoadMore = false;
                        page--;
                    }
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                refreshListView.onRefreshComplete();
                loadingDialog.dismiss();
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.view_no_data, null);
                refreshListView.setEmptyView(view);
                if (isLoadMore) {
                    isLoadMore = false;
                    page--;
                }
            }
        });
    }


    private void myInterface() {
        mAdapter.setOrderDelete(new OrderAdapter.OrderDelete() {
            @Override
            public void delete() {
                deletePopupwindow();
            }
        });

        mAdapter.setOrderPay(new OrderAdapter.OrderPay() {
            @Override
            public void pay(int i, String OrderNo, String GoodsName, String GoodsPrice) {
                dia(i, OrderNo, GoodsName, GoodsPrice);
            }
        });

        mAdapter.setOrderCancal(new OrderAdapter.OrderCancal() {
            @Override
            public void cancal() {
                quxiaoPopupwindow();
            }
        });


    }

    //我是商家中的订单管理
    private void loadSeller() {
        RequestParams params = new RequestParams();

        loadingDialog.setMsg("请稍等");
        loadingDialog.show();
        params.addBodyParameter("tsId", SPHelper.getTSId());
        if (position == 5) {
            params.addBodyParameter("status", "6");
        } else {
            params.addBodyParameter("status", position + "");
        }
        params.addBodyParameter("page", page + "");
        activity.loadData(HttpRequest.HttpMethod.POST, ApiDefine.STORE_ORDER_LIST, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                loadingDialog.dismiss();
                refreshListView.onRefreshComplete();
                String response = responseInfo.result;
                Log.e(TAG, "laod all seller:" + response);

                try {
                    JSONObject object = new JSONObject(response);
                    if (object.getString("code").equals("100")) {
                        aShopList = new JSONArray();
                        aShopList = object.getJSONArray("list");
                        if (aShopList.length() > 0) {
                            if (isLoadMore) {
                                isLoadMore = false;
                            } else {
                                shopList.clear();
                            }
                            shopArray = aShopList;
                            shops(aShopList);
//                            shopList = new ArrayList<>();
                            for (int shop = 0; shop < aShopList.length(); shop++) {
                                try {
                                    shopList.add(aShopList.getJSONObject(shop));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

//                            refreshListView.setAdapter(mAdapter);
                            mAdapter.setData(shopList, position);

                            mAdapter.notifyDataSetChanged();
                            mAdapter.setOrderManage(new OrderAdapter.OrderManage() {
                                @Override
                                public void order() {
                                    orderPopupwindow();
                                }
                            });

                            mAdapter.setOrderCancal(new OrderAdapter.OrderCancal() {
                                @Override
                                public void cancal() {
                                    quxiaoPopupwindow();
                                }
                            });
                        } else {
                            if (isLoadMore) {
                                isLoadMore = false;
                                page--;
                            }else{
                                View view=LayoutInflater.from(getActivity()).inflate(R.layout.view_no_data,null);
                                refreshListView.setEmptyView(view);
                            }
                        }
                    } else {
                        if (isLoadMore) {
                            isLoadMore = false;
                            page--;
                        } else {
                            View view=LayoutInflater.from(getActivity()).inflate(R.layout.view_no_data,null);
                            refreshListView.setEmptyView(view);
                            shopList.clear();
                            mAdapter.setData(shopList, position);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                } catch (Exception e) {
                    if (isLoadMore) {
                        isLoadMore = false;
                        page--;
                    }
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                refreshListView.onRefreshComplete();
                View view=LayoutInflater.from(getActivity()).inflate(R.layout.view_no_data,null);
                refreshListView.setEmptyView(view);
                loadingDialog.dismiss();
                if (isLoadMore) {
                    isLoadMore = false;
                    page--;
                }
            }

        });
    }

    //收货
    private void dia(int i, String OrderNo, String GoodsName, String GoodsPrice) {
        PayOrderInfo orderInfo = new PayOrderInfo();
        orderInfo.setOrderNo(OrderNo);
        orderInfo.setGoodsName(GoodsName);
        orderInfo.setGoodsDesc("这是测试");
        orderInfo.setGoodsPrice(GoodsPrice);
        if (i == 2) {
            orderInfo.setType("receive");
        }
        checkPassWord(orderInfo);
//        if (true) {
//            // SPHelper.setToId(toId);
//            AccountPay pay = new AccountPay(Integer.parseInt(SPHelper.getToId()));
//            pay.pay(getActivity(), orderInfo, (OnPayListener) getActivity());
//            if ("member".equals(SPHelper.getTYPE())) {
//                loadOrder();
//            } else if ("seller".equals(SPHelper.getTYPE())) {
//                loadSeller();
//            }
//        } else {
//            MyDialogTextView dialog = new MyDialogTextView(getActivity(), "提示", "您还未设置支付密码，为保护账户安全，请先设置支付密码!");
//            dialog.setDialogCallback(new MyDialogTextView.DialogCallback() {
//                @Override
//                public void dialogOpt() {
//                    Intent intent = new Intent(getActivity(), SettingBalanceActivity.class);
//                    startActivityForResult(intent, Constant.SETTING_BALANCE_REQUEST);
//                }
//            });
//            dialog.show();
//
//        }
    }

    //检查是否设置了余额支付密码
    private void checkPassWord(final PayOrderInfo orderInfo) {
        loadingDialog.setMsg("正在确认订单");
        loadingDialog.show();
        RequestParams params = new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        activity.loadData(HttpRequest.HttpMethod.POST, ApiDefine.CHECK_PAY_PASS, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                loadingDialog.dismiss();
                Log.e("TAG", "cheek PassWord response:" + responseInfo.result);
                if (TextUtils.isEmpty(responseInfo.result)) {
                    activity.showMsg("检查支付密码失败");
                } else {
                    try {
                        JSONObject root = new JSONObject(responseInfo.result);
                        String code = root.getString("code");
                        if ("100".equals(code)) {
                            //去确认收货
                            AccountPay pay = new AccountPay(1);
//                            TODO 需要修改
//                            pay.pay(getActivity(), orderInfo, OrderListFragment.this);
                        } else {
                            MyDialogTextView dialog = new MyDialogTextView(activity, "提示", "您还未设置支付密码，为保护账户安全，请先设置支付密码!");
                            dialog.setDialogCallback(new MyDialogTextView.DialogCallback() {
                                @Override
                                public void dialogOpt() {
                                    Intent intent = new Intent(activity, SettingBalanceActivity.class);
                                    startActivityForResult(intent, Constant.SETTING_BALANCE_REQUEST);
                                }
                            });
                            dialog.show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                loadingDialog.dismiss();
                Log.e("TAG", "request failure :" + s);
                activity.showMsg("当前网络不可用，请检查您的网设置");
            }
        });
    }


    private List<ShopModel> shops(JSONArray shops) {
        List<ShopModel> shop = new ArrayList<>();
        List<MyGoodsModel> list = new ArrayList<>();

        shopModel = new ShopModel();
        myGoodsModel = new MyGoodsModel();
        for (int i = 0; i < shops.length(); i++) {
            try {
                JSONObject shopObject = shops.getJSONObject(i);
                shopModel.setTsId(shopObject.getString("tsId"));
                shopModel.setTsName(shopObject.getString("tsName"));
                shopModel.setToId(shopObject.getString("toId"));
                shopModel.setToNumber(shopObject.getString("toNumber"));
                shopModel.setToPayPrice(shopObject.getString("toPayPrice"));
                shopModel.setToTotalCount(shopObject.getString("toTotalCount"));
                shopModel.setToStatus(shopObject.getString("toStatus"));
                shopModel.setToType(shopObject.getString("toType"));
                JSONArray goodsArray = shopObject.getJSONArray("products");
                for (int g = 0; g < goodsArray.length(); g++) {
                    JSONObject goodsObject = goodsArray.getJSONObject(g);
                    myGoodsModel.setTpId(goodsObject.getString("tpId"));
                    myGoodsModel.setTpName(goodsObject.getString("tpName"));
                    myGoodsModel.setTpPic(goodsObject.getString("tpPic"));
                    myGoodsModel.setTodPrice(goodsObject.getString("todPrice"));
                    myGoodsModel.setTodCount(goodsObject.getString("todCount"));
                    list.add(myGoodsModel);
                }
                shopModel.setMyGoodsModels(list);
                shop.add(shopModel);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

//        for (int t = 0; t < shop.size(); t++) {
//            Log.e(TAG, t + "   " + shop.get(t).getTsName());
//        }

        return shop;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.e("tag", "PullToRefreshListView   的点击事件");
        Log.e("tag", "PullToRefreshListView   i==" + "");
        shopList.get(i - 1);
        id = i;

        Intent intent = new Intent(getActivity(), MyOrderDetailsActivity.class);
        intent.putExtra("shopList", shopList.get(i - 1).toString());
        startActivity(intent);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mBaseActivity = (BaseActivity) activity;
    }

    @Override
    public void onRefresh(PullToRefreshBase refreshView) {
        if (refreshView.isHeaderShown()) {
            isLoadMore = false;
            page = 1;
            refreshListView.setAdapter(null);
            refreshListView.setAdapter(mAdapter);
            if ("member".equals(SPHelper.getTYPE())) {

                loadOrder();
            } else if ("seller".equals(SPHelper.getTYPE())) {
                loadSeller();
            }
        } else if (refreshView.isFooterShown()) {
            isLoadMore = true;
            page++;
            if ("member".equals(SPHelper.getTYPE())) {

                loadOrder();
            } else if ("seller".equals(SPHelper.getTYPE())) {
                loadSeller();
            }
            refreshListView.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载");
            refreshListView.getLoadingLayoutProxy(false, true).setRefreshingLabel("加载中...");
            refreshListView.getLoadingLayoutProxy(false, true).setReleaseLabel("松开加载");
        }
    }

    private PopupWindow popupWindow = null;
    EditText edit_name, edit_num;

    @SuppressLint("InflateParams")
    public void orderPopupwindow() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.popupwindow_edittext,
                null);

        edit_name = (EditText) view.findViewById(R.id.edit_name);
        edit_num = (EditText) view.findViewById(R.id.edit_num);


        TextView txt_cancel = (TextView) view.findViewById(R.id.txt_cancel);
        txt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                popupWindow.dismiss();

            }
        });
        TextView txt_confirm = (TextView) view.findViewById(R.id.txt_confirm);
        txt_confirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                strName = edit_name.getText().toString();
                strNum = edit_num.getText().toString();
                loadSend();
                if ("member".equals(SPHelper.getTYPE())) {
                    loadOrder();
                } else if ("seller".equals(SPHelper.getTYPE())) {
                    loadSeller();
                }
                popupWindow.dismiss();

            }
        });

        popupWindow = new PopupWindow(view, ViewPager.LayoutParams.MATCH_PARENT,
                ViewPager.LayoutParams.MATCH_PARENT, true);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAtLocation(refreshListView, Gravity.CENTER, 0, 0);

    }

    public void quxiaoPopupwindow() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.my_dialog_text,
                null);

        TextView text_dialog_title, text_dialog_content;
        Button btn_cancel, btn_confirm;

        text_dialog_title = (TextView) view.findViewById(R.id.text_dialog_title);
        text_dialog_content = (TextView) view.findViewById(R.id.text_dialog_content);

        btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        btn_confirm = (Button) view.findViewById(R.id.btn_confirm);

        text_dialog_title.setText("取消订单");
        text_dialog_content.setText("您确定取消订单吗？");
        text_dialog_title.setTextSize(17f);
        text_dialog_content.setTextSize(14f);
        btn_cancel.setTextColor(getResources().getColor(R.color.dialog_btn_color));
        btn_confirm.setTextColor(getResources().getColor(R.color.dialog_btn_color));
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                loadCancal();
                if ("member".equals(SPHelper.getTYPE())) {
                    loadOrder();
                } else if ("seller".equals(SPHelper.getTYPE())) {
                    loadSeller();
                }
                popupWindow.dismiss();
            }
        });


        popupWindow = new PopupWindow(view, ViewPager.LayoutParams.WRAP_CONTENT,
                500, true);


        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAtLocation(refreshListView, Gravity.CENTER, 0, 0);

    }

    public void deletePopupwindow() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.my_dialog_text,
                null);

        TextView text_dialog_title, text_dialog_content;
        Button btn_cancel, btn_confirm;

        text_dialog_title = (TextView) view.findViewById(R.id.text_dialog_title);
        text_dialog_content = (TextView) view.findViewById(R.id.text_dialog_content);

        btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        btn_confirm = (Button) view.findViewById(R.id.btn_confirm);

        text_dialog_title.setText("删除订单");
        text_dialog_content.setText("您确定删除订单吗？");
        text_dialog_title.setTextSize(17f);
        text_dialog_content.setTextSize(14f);
        btn_cancel.setTextColor(getResources().getColor(R.color.dialog_btn_color));
        btn_confirm.setTextColor(getResources().getColor(R.color.dialog_btn_color));
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                loadDelete();
                if ("member".equals(SPHelper.getTYPE())) {
                    loadOrder();
                } else if ("seller".equals(SPHelper.getTYPE())) {
                    loadSeller();
                }
                popupWindow.dismiss();
            }
        });


        popupWindow = new PopupWindow(view, ViewPager.LayoutParams.WRAP_CONTENT,
                500, true);


        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAtLocation(refreshListView, Gravity.CENTER, 0, 0);

    }


    private void loadCancal() {
        loadingDialog = new LoadingDialog(getActivity());
        loadingDialog.setMsg("请稍等");
        loadingDialog.showDialog();
        RequestParams params = new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        params.addBodyParameter("toId", SPHelper.getToId());

        activity.loadData(HttpRequest.HttpMethod.POST, ApiDefine.ORDER_CANCEL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String response = responseInfo.result;

                Log.e(TAG, TAG + "   " + response);
                try {
                    JSONObject object = new JSONObject(response);
                    activity.showMsg(object.getString("message"));
                    if ("100".equals(object.getString("code"))) {

                        if ("member".equals(SPHelper.getTYPE())) {
                            loadOrder();
                        } else if ("seller".equals(SPHelper.getTYPE())) {
                            loadSeller();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                loadingDialog.dismiss();

            }

            @Override
            public void onFailure(HttpException e, String s) {
                loadingDialog.dismiss();
            }
        });
    }

    private void loadSend() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        params.addBodyParameter("toId", SPHelper.getToId());
        params.addBodyParameter("company", strName);
        params.addBodyParameter("number", strNum);
        activity.loadData(HttpRequest.HttpMethod.POST, ApiDefine.STORE_SEND, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String response = responseInfo.result;
                Log.e(TAG, TAG + "     " + response);
                try {
                    JSONObject object = new JSONObject(response);
                    activity.showMsg(object.getString("message"));
                    if ("100".equals(object.getString("code"))) {
                        //refreshListView.removeViewAt(id);

                        if ("member".equals(SPHelper.getTYPE())) {
                            loadOrder();
                        } else if ("seller".equals(SPHelper.getTYPE())) {
                            loadSeller();
                        }
                        mAdapter.notifyDataSetChanged();
                        popupWindow.dismiss();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    private void loadDelete() {

        RequestParams params = new RequestParams();
        params.addBodyParameter("toId", SPHelper.getToId());
        if ("member".equals(SPHelper.getTYPE())) {
            params.addBodyParameter("tmId", SPHelper.getTmId());
            activity.loadData(HttpRequest.HttpMethod.POST, ApiDefine.ORDER_DELTE, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    String response = responseInfo.result;
                    Log.e(TAG, TAG + "     " + response);
                    Log.e(TAG, TAG + "toId     " + SPHelper.getToId());
                    try {
                        JSONObject object = new JSONObject(response);
                        activity.showMsg(object.getString("message"));
                        if ("100".equals(object.getString("code"))) {

                            if ("member".equals(SPHelper.getTYPE())) {
                                loadOrder();
                            } else if ("seller".equals(SPHelper.getTYPE())) {
                                loadSeller();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    loadingDialog.dismiss();
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    loadingDialog.dismiss();
                }
            });
        } else if ("seller".equals(SPHelper.getTYPE())) {
            params.addBodyParameter("tmId", SPHelper.getTmId());
            params.addBodyParameter("password", new SelfUtils().getMD5(SPHelper.getPassword()));
            activity.loadData(HttpRequest.HttpMethod.POST, ApiDefine.STORE_ORDER_DELET, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    String response = responseInfo.result;
                    Log.e(TAG, TAG + "   " + response);
                    try {
                        JSONObject object = new JSONObject(response);
                        activity.showMsg(object.getString("message"));
                        if ("100".equals(object.getString("code"))) {

                            if ("member".equals(SPHelper.getTYPE())) {
                                loadOrder();
                            } else if ("seller".equals(SPHelper.getTYPE())) {
                                loadSeller();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    loadingDialog.dismiss();
                }

                @Override
                public void onFailure(HttpException e, String s) {

                    loadingDialog.dismiss();
                }
            });
        }
    }

    @Override
    public void onPayStart(PayType payType) {
        Log.e("TAG", "pay start");
    }

    @Override
    public void onPaySuccessed(PayType payType, String s) {
        Log.e("TAG", "pay success to refersh");
        page = 1;
        isLoadMore = false;
        position = 3;
        if ("member".equals(SPHelper.getTYPE())) {
            loadOrder();
        } else if ("seller".equals(SPHelper.getTYPE())) {
            loadSeller();
        }
    }

    @Override
    public void onPayFailed(PayType payType, String s) {
        Log.e("TAG", "pay failed");
    }

    @Override
    public void onPayCanceled(PayType payType) {
        Log.e("TAG", "pay cancel");
    }

    @Override
    public void onPayConfirming(PayType payType) {
        Log.e("TAG", "pay confirm");
    }
}